
/*! \file   RuleIdSource.h

    Copyright 2003-2004 Fraunhofer Institute for Open Communication Systems (FOKUS),
                        Berlin, Germany

    This file is part of Network Measurement and Accounting System (NETMATE).

    NETMATE is free software; you can redistribute it and/or modify 
    it under the terms of the GNU General Public License as published by 
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    NETMATE is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this software; if not, write to the Free Software 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Description:
    manage unique numeric rule id space

    $Id: RuleIdSource.h 748 2009-09-10 02:54:03Z szander $
*/

#ifndef _RULEIDSOURCE_H_
#define _RULEIDSOURCE_H_


#include "stdincpp.h"
#include "Error.h"
#include "Logger.h"


/*! \short   generate unique id numbers
  
    The RuleIdSource class can generate unique 32 bit integer numbers for
    use by other functions. A single number can be lent from the pool of
    available numbers, and this number will not be generated by a call to 
    the newId function until it has been previously released with a call
    to the freeId function.
*/

class RuleIdSource
{
  private:

    unsigned long long num;
    int unique;
    list<unsigned long long> freeIds; //!< list of previously freed (now unused) ids
    
  public:

    //! construct and initialize a RuleIdSource object
    // if unique is set to 1 Ids should be unique until we wrap around 2^63
    RuleIdSource(int unique = 0);

    //! destroy a RuleIdSource object
    ~RuleIdSource();

    /*! \short   generate a new internal id number

        return a new Id value that is currently not in use. This value will be
        marked as used and will not returned by a call to newId unless it has
        been released again with a call to freeId
        \returns unique unused id value
    */
    unsigned long long newId( void );

    /*! \short   release an id number

        The released id number can be reused (i.e. returned by newId) after
        the call to freeId. Do not release numbers that have not been obtained
        by a call to newId this will result in unpredictable behaviour.

        \arg \c id - id value that is to be released for future use
    */
    void freeId( unsigned long long id );

    //! dump a RuleIdSource object
    void dump( ostream &os );

};


//! overload for <<, so that a RuleIdSource object can be thrown into an iostream
ostream& operator<< ( ostream &os, RuleIdSource &ris );


#endif // _RULEIDSOURCE_H_
